package com.es.service.impl.impl;

import com.es.entity.Member;
import com.es.mapper.MemberMapper;
import com.es.mapper.OrderMapper;
import com.es.mybatis.util.MybatisUtils;
import com.es.service.MemberService;
import org.apache.ibatis.session.SqlSession;

import java.util.List;

/**
 * 描述:
 * MyBatis-
 *
 * @author Derek
 * 创建时间：2022-05-09 11:59
 */
public class MemberServiceImpl implements MemberService {
    @Override
    public int save(Member member) {

        //
        SqlSession session = MybatisUtils.getSqlSession(false);
        MemberMapper mapper = session.getMapper(MemberMapper.class);
        //主键是自动增长，
        int i = mapper.insert(member);
        OrderMapper orderMapper = session.getMapper(OrderMapper.class);
        member.getOrders().forEach(
                o->{
                    orderMapper.insert(o);
                }
        );

        session.commit();
        session.close();
        return 0;
    }

    @Override
    public Member get(Integer id) {
        SqlSession session = MybatisUtils.getSqlSession(false);
        MemberMapper mapper = session.getMapper(MemberMapper.class);
        return mapper.get(id);
    }

    @Override
    public List<Member> list() {
        SqlSession session = MybatisUtils.getSqlSession(false);
        MemberMapper mapper = session.getMapper(MemberMapper.class);
        return mapper.list();
    }
}
